package com.ld.hpfs.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ld.hpfs.pojo.DbSourceSetting;
import com.ld.hpfs.service.DbSourceSettingService;
import com.ld.hpfs.dao.DbSourceSettingMapper;

@Service
public class DbSourceSettingServiceImpl implements DbSourceSettingService {

	@Autowired
	private DbSourceSettingMapper dbsourceSettingMapper;

	public int addDbSourceSetting(DbSourceSetting setting) {
		java.util.HashMap<String, Object> params = new java.util.HashMap<String, Object>();
		params.put("DS_TYPE", setting.getDbsourceType());
		params.put("DS_DB_SID", setting.getInstance());
		params.put("DS_USERNAME", setting.getDbusername());
		params.put("DS_PASSWORD", setting.getDbpassword());
		params.put("DS_IP", setting.getDbhostname());
		params.put("DS_PORT", setting.getDbport());
		params.put("DS_CONNECT_STR", setting.getDbconnectstring());
		params.put("MAPPING_STR", setting.getDbcolumnMapping());
		params.put("COMPANY_ID", String.valueOf(setting.getCompanyId()));
		params.put("DS_NAME", setting.getDbdesc());
		params.put("SQL_STR", setting.getDbsql().replaceAll("\n", " "));
		params.put("CREATOR_ID", setting.getCreatorId()); // TODO: GET
															// CREATOR_ID

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		// format.setTimeZone(TimeZone.getTimeZone( "GMT-0"));
		String date = format.format(new Date());
		params.put("CREATE_TIME", date);

		return dbsourceSettingMapper.addDbSourceSetting(params);

	}

	public List<DbSourceSetting> getDbSourceSettings(
			HashMap<String, Object> param) {
		// DbSourceSetting dbsetting = new DbSourceSetting();
		// dbsetting.setCompanyId(1);
		// dbsetting.setDbhostname("sqlserver.lordar.com");
		// dbsetting.setDbport("1433");
		// dbsetting.setDbusername("sa");
		// dbsetting.setDbpassword("lordar@2012");
		// dbsetting.setInstance("HPFSDB");
		// dbsetting.setDbsourceType("sqlserver");
		//
		// DbSourceSetting dbsetting1 = new DbSourceSetting();
		// dbsetting1.setCompanyId(1);
		// dbsetting1.setDbhostname("sqlserver.lordar.com");
		// dbsetting1.setDbport("1433");
		// dbsetting1.setDbusername("sa");
		// dbsetting1.setDbpassword("lordar@2012");
		// dbsetting1.setInstance("HPFSDB");
		// dbsetting1.setDbsourceType("sqlserver");

		int count = dbsourceSettingMapper.getListCount(param);
		List<DbSourceSetting> result = new ArrayList<DbSourceSetting>();
		if (count == 0) {
			return null;
		} else {
			param.put("pageCount", count);
			ArrayList<HashMap<String, Object>> rawResult = dbsourceSettingMapper
					.getDbSourceSettings(param);

			int companyId = Integer.parseInt(param.get("id").toString());
			if (rawResult != null) {
				for (int i = 0; i < rawResult.size(); i++) {
					HashMap<String, Object> beanMap = rawResult.get(i);
					DbSourceSetting dbsetting = new DbSourceSetting();

					dbsetting.setCompanyId(companyId);
					dbsetting.setDbhostname(beanMap.get("DS_IP").toString());
					dbsetting.setDbport(beanMap.get("DS_PORT").toString());
					dbsetting.setDbusername(beanMap.get("DS_USERNAME")
							.toString());
					dbsetting.setDbpassword(beanMap.get("DS_PASSWORD")
							.toString());
					dbsetting.setInstance(beanMap.get("DS_DB_SID").toString());
					dbsetting
							.setDbsourceType(beanMap.get("DS_TYPE").toString());
					// dbsetting.setDbsourceName(beanMap.get("DS_NAME").toString());
					dbsetting.setDbconnectstring(beanMap.get("DS_CONNECT_STR")
							.toString());
					dbsetting.setDbcolumnMapping(beanMap.get("MAPPING_STR")
							.toString());
					dbsetting.setDbdesc(beanMap.get("DS_NAME").toString());
					dbsetting.setId(Integer.parseInt(beanMap.get("DS_HID")
							.toString()));
					dbsetting.setDbsql(beanMap.get("SQL_STR").toString());

					dbsetting
							.setCreatorId(beanMap.get("CREATOR_ID").toString());
					dbsetting.setCreateTime(beanMap.get("CREATE_TIME")
							.toString());

					dbsetting.setId(Integer.parseInt(beanMap.get("DS_HID")
							.toString()));

					result.add(dbsetting);
				}
			}
		}

		return result;
	}

	public DbSourceSetting getDbSourceSetting(int id) {
		HashMap<String, Object> beanMap = dbsourceSettingMapper
				.getDbSourceSetting(id);

		if (beanMap != null) {
			DbSourceSetting dbsetting = new DbSourceSetting();
			dbsetting.setCompanyId(Integer.parseInt(beanMap.get("COMPANY_ID")
					.toString()));
			dbsetting.setDbhostname(beanMap.get("DS_IP").toString());
			dbsetting.setDbport(beanMap.get("DS_PORT").toString());
			dbsetting.setDbusername(beanMap.get("DS_USERNAME").toString());
			dbsetting.setDbpassword(beanMap.get("DS_PASSWORD").toString());
			dbsetting.setInstance(beanMap.get("DS_DB_SID").toString());
			dbsetting.setDbsourceType(beanMap.get("DS_TYPE").toString());
			// dbsetting.setDbsourceName(beanMap.get("DS_NAME").toString());
			dbsetting.setDbconnectstring(beanMap.get("DS_CONNECT_STR")
					.toString());
			dbsetting.setDbcolumnMapping(beanMap.get("MAPPING_STR").toString());
			dbsetting.setDbdesc(beanMap.get("DS_NAME").toString());
			dbsetting.setId(Integer.parseInt(beanMap.get("DS_HID").toString()));
			dbsetting.setDbsql(beanMap.get("SQL_STR").toString());

			dbsetting.setCreatorId(beanMap.get("CREATOR_ID").toString());
			dbsetting.setCreateTime(beanMap.get("CREATE_TIME").toString());
			dbsetting.setId(Integer.parseInt(beanMap.get("DS_HID").toString()));
			return dbsetting;
		}

		return null;

	}

	public int deleteDbSourceSetting(int id) {
		return dbsourceSettingMapper.deleteDbSourceSetting(id);

	}

	public int updateDbSourceSetting(DbSourceSetting setting) {
		java.util.HashMap<String, Object> params = new java.util.HashMap<String, Object>();
		params.put("DS_TYPE", setting.getDbsourceType());
		params.put("DS_DB_SID", setting.getInstance());
		params.put("DS_USERNAME", setting.getDbusername());
		params.put("DS_PASSWORD", setting.getDbpassword());
		params.put("DS_IP", setting.getDbhostname());
		params.put("DS_PORT", setting.getDbport());
		params.put("DS_CONNECT_STR", setting.getDbconnectstring());
		params.put("MAPPING_STR", setting.getDbcolumnMapping());
		params.put("COMPANY_ID", String.valueOf(setting.getCompanyId()));
		params.put("DS_NAME", setting.getDbdesc());
		params.put("DS_HID", setting.getId().toString());
		params.put("SQL_STR", setting.getDbsql().replace("\n", " "));
		return dbsourceSettingMapper.updateDbSourceSetting(params);

	}
}
